package com.mine.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mine.pojo.Order;
import com.mine.pojo.OrderItem;
import com.mine.pojo.User;
import com.mine.service.CartService;
import com.mine.service.MealService;
import com.mine.service.OrderService;
import com.mine.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
public class OrderController {

    @Autowired()
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    CartService cartService;
    @Autowired
    MealService mealService;

    //*从购物车转到订单页面
    @RequestMapping("/orderByMoney")
    public String orderByMoney(String num, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        //用户的购物车
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByUser(user);
        model.addAttribute("orderItemList", orderItemList);
        model.addAttribute("num", num);
        return "meal/order";
    }

    //*订单提交成功，转到提示成功页面
    @RequestMapping("/order/submit")
    public String submitOrder(Order order, String money, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (order.getOrder_remarks() == null || "".equals(order.getOrder_remarks())) {
            order.setOrder_remarks("无");
        }
        orderService.addOrder(user, money, order);
        return "redirect:/order/history";
    }

    //*去支付
    @RequestMapping("/order/pay")
    @ResponseBody
    public String pay(String order_id){
        orderService.paySuccess(order_id);
        return "OK";
    }

    //*查看历史订单列表
    @RequestMapping("/order/history")
    public String orderHistory(HttpSession session, Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        //分页，每页7个
        PageHelper.startPage(pageNum, 7);
        List<Order> orderList = orderService.queryAllOrder(user, null);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("orderList", orderList);
        return "order/order_list";
    }

    //*取消订单
    @RequestMapping("overOrder")
    @ResponseBody
    public String overOrder(Integer order_id) {
        orderService.overOrder(order_id);
        return "OK";
    }

    //*查看订单
    @RequestMapping("LookOrder")
    public String LookOrder(String invalid, String order_id, Model model, String full) {
        if (invalid != null && !"".equals(invalid)) {
            model.addAttribute("invalid", invalid);
        }
        Order order = orderService.queryOrderById(order_id);
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByOrder(order_id);
        model.addAttribute("orderItemList", orderItemList);
        model.addAttribute("order", order);
        model.addAttribute("full", full);
        return "order/order_message";
    }

    //我的评论（先去完成订单的页面）
    @RequestMapping("user_message")
    public String user_message(String mm, HttpSession session, Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
        User user = (User) session.getAttribute("user");
        if (user == null) { //如果找不到用户，转到登录页
            model.addAttribute("msg", "请先登录");
            return "login";
        }
        if (mm != null && !"".equals(mm)) {
            model.addAttribute("mm", "评论成功");
        }
        //分页，每页7个
        PageHelper.startPage(pageNum, 7);
        List<Order> orderList = orderService.queryAllOrder(user, "1");
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("orderList", orderList);
        return "user/user_message";
    }

}
